package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wanmait.employment.aop.LogInfo;
import com.wanmait.employment.entity.Admin;
import com.wanmait.employment.entity.Industry;
import com.wanmait.employment.entity.PositionType;
import com.wanmait.employment.mapper.AdminMapper;
import com.wanmait.employment.mapper.IndustryMapper;
import com.wanmait.employment.mapper.PositionTypeMapper;
import com.wanmait.employment.service.PositionTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)

public class PositionTypeServiceImpl implements PositionTypeService {
    @Resource
    private PositionTypeMapper positionTypeMapper;
    @Resource
    private IndustryMapper industryMapper;
    @Resource
    private AdminMapper adminMapper;
    @Override
    public PageInfo<PositionType> select(Integer industryId,String title, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<PositionType> queryWrapper=new QueryWrapper<>();

        queryWrapper.like(StringUtil.isNotEmpty(title),"title",title);
        queryWrapper.eq(industryId!=null,"industry_id",industryId);
        List<PositionType> positionTypeList= positionTypeMapper.selectList(queryWrapper);
        PageInfo<PositionType> pageInfo=new PageInfo<>(positionTypeList);
        pageInfo.getList().forEach(positionType -> {
            QueryWrapper<Industry> industryQueryWrapper=new QueryWrapper<>();
            industryQueryWrapper.eq("id",positionType.getIndustryId());
            Industry industry=industryMapper.selectOne(industryQueryWrapper);
            positionType.setIndustry(industry);

            QueryWrapper<Admin> adminQueryWrapper=new QueryWrapper<>();
            adminQueryWrapper.eq("id",positionType.getAdminId());
            Admin admin=adminMapper.selectOne(adminQueryWrapper);
            positionType.setAdmin(admin);
        });
        return pageInfo;
    }

    @LogInfo("更新/添加主职位类型")
    @Override
    public Boolean update(PositionType positionType) {
        positionType.setIndustryId(positionType.getIndustry().getId());
        if(positionType.getId()!=null){

            positionTypeMapper.updateById(positionType);
            return true;
        }
        System.out.println(positionType);

        positionTypeMapper.insert(positionType);
        return true;
    }

    @LogInfo("删除主职位类型")
    @Override
    public void delete(Integer id) {
        positionTypeMapper.deleteById(id);
    }

    @Override
    public List<PositionType> listAll() {
        List<PositionType> positionTypeList=positionTypeMapper.selectList(null);
        return positionTypeList;
    }

    @Override
    public List<PositionType> getListByIndustryId(Integer id) {
        LambdaQueryWrapper<PositionType> positionTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        positionTypeLambdaQueryWrapper.eq(PositionType::getIndustryId,id);
        List<PositionType> positionTypeList=positionTypeMapper.selectList(positionTypeLambdaQueryWrapper);
        return positionTypeList;
    }
}
